package com.ruoyi.record.controller;

import java.util.List;

import com.ruoyi.api.system.utils.ShiroUtils;
import com.ruoyi.record.domain.RecordAttend;
import com.ruoyi.record.service.IRecordAttendService;
import com.ruoyi.common.utils.DateUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.record.domain.RecordAttendContent;
import com.ruoyi.record.service.IRecordAttendContentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 考勤详情Controller
 * 
 * @author ruoyi
 * @date 2022-09-26
 */
@Controller
@RequestMapping("/record/attend/content")
public class RecordAttendContentController extends BaseController
{
    private String prefix = "record/attend/content";

    @Autowired
    private IRecordAttendService recordAttendService;

    @Autowired
    private IRecordAttendContentService recordAttendContentService;

    @GetMapping("/{attendId}")
    public String content(@PathVariable(value = "attendId") Long attendId, ModelMap mmap)
    {
        RecordAttend attend = recordAttendService.selectRecordAttendByAttendId(attendId);
        mmap.put("attend", attend);
        return prefix + "/content";
    }

    /**
     * 查询考勤详情列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RecordAttendContent recordAttendContent)
    {
        startPage();
        List<RecordAttendContent> list = recordAttendContentService.selectRecordAttendContentList(recordAttendContent);
        return getDataTable(list);
    }

    /**
     * 新增考勤详情
     */
    @GetMapping("/add/{attendId}")
    public String add(@PathVariable("attendId") Long attendId, ModelMap mmap)
    {
        RecordAttend attend = recordAttendService.selectRecordAttendByAttendId(attendId);
        mmap.put("attend", attend);
        List dateList = DateUtils.getDateList(attend.getAttendMonth());
        mmap.put("dateList", dateList);
        return prefix + "/add";
    }

    /**
     * 新增保存考勤详情
     */
    @Log(title = "考勤详情", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RecordAttendContent recordAttendContent)
    {
        if(recordAttendContentService.checkRecordAttendContent(recordAttendContent)) {
            recordAttendContent.setCreateBy(ShiroUtils.getLoginName());
            return toAjax(recordAttendContentService.insertRecordAttendContent(recordAttendContent));
        }else{
            return error("当前选中的人员已存在考勤记录");
        }
    }

    /**
     * 修改考勤详情
     */
    @GetMapping("/edit/{contentId}")
    public String edit(@PathVariable("contentId") Long contentId, ModelMap mmap)
    {
        RecordAttendContent recordAttendContent = recordAttendContentService.selectRecordAttendContentByContentId(contentId);
        mmap.put("recordAttendContent", recordAttendContent);
        RecordAttend attend = recordAttendService.selectRecordAttendByAttendId(recordAttendContent.getAttendId());
        List dateList = DateUtils.getDateList(attend.getAttendMonth());
        mmap.put("dateList", dateList);
        return prefix + "/edit";
    }

    /**
     * 修改保存考勤详情
     */
    @Log(title = "考勤详情", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RecordAttendContent recordAttendContent)
    {
        if(recordAttendContentService.checkRecordAttendContent(recordAttendContent)) {
            recordAttendContent.setUpdateBy(ShiroUtils.getLoginName());
            return toAjax(recordAttendContentService.updateRecordAttendContent(recordAttendContent));
        }else {
            return error("当前人员已存在考勤记录");
        }
    }

    /**
     * 删除考勤详情
     */
    @Log(title = "考勤详情", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(recordAttendContentService.deleteRecordAttendContentByContentIds(ids));
    }

    /**
     * 校验表号唯一性
     */
    @PostMapping("/checkRecordAttendContent")
    @ResponseBody
    public AjaxResult checkRecordAttendContent(RecordAttendContent recordAttendContent)
    {
        boolean checkFlag = recordAttendContentService.checkRecordAttendContent(recordAttendContent);
        return checkFlag ? success() : error();
    }
}
